﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class MapManager : MonoBehaviour
{

    public static MapManager _instance;

    public int MapX;
    public int MapY;
    public GameObject MapGrid;

    private Dictionary<int, GameObject> mapGridDict = new Dictionary<int, GameObject>();
    private ArrayList lastFoodAttackRange = new ArrayList();
    private ArrayList BlueMapGrid = new ArrayList();

    /// <summary>
    /// 初始化地图
    /// </summary>
    void Awake()
    {
        _instance = this;

        for (int a = 0; a < MapX; a++)
        {
            for (int b = 0; b < MapY; b++)
            {
                GameObject go = Instantiate(MapGrid) as GameObject;
                go.GetComponent<MapGridView>().Init(a, b);
                int temp = a * MapY + b;
                mapGridDict.Add(temp, go);
            }
        }
    }

    /// <summary>
    /// 获取地图格子的脚本信息
    /// </summary>
    /// <param name="mapPos"></param>
    /// <returns></returns>
    public GameObject GetMapGridGo(int mapNum)
    {
        GameObject img = null;

        mapGridDict.TryGetValue(mapNum, out img);

        return img;
    }

    /// <summary>
    /// 根据地图格子的坐标算出地图格子的ID编号
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public int CalcuMapGridID(int x, int y)
    {
        int mapNum = 0;

        mapNum = x * MapY + y;

        return mapNum;
    }

    /// <summary>
    /// 当食物有充能技能的时候再调用
    /// </summary>
    /// <param name="foodlist"></param>
    //public void SetMapGridState(List<Food> foodlist)
    //{
    //    //清除原有的
    //    for (int i = 0; i < lastFoodAttackRange.Count; i++)
    //    {
    //        GameObject mapgrid = null;
    //        mapgrid = GetMapGridGo((int)lastFoodAttackRange[i]);
    //        mapgrid.GetComponent<MapGridView>().SetState(MapGridState.normal);
    //    }
    //    lastFoodAttackRange.Clear();

    //    //变蓝
    //    for (int i = 1; i < foodlist.Count; i++)
    //    {
    //        int temp = CalcuMapGridID(foodlist[i].foodPosX, foodlist[i].foodPosY);

    //        foreach (int a in foodlist[0].AttackRangeMapGridID)
    //        {
    //            if (temp == a)
    //            {
    //                foodlist[i].isAttack = true;
    //                for (int v = 0; v < foodlist[i].AttackRangeMapGridID.Count; v++)
    //                {
    //                    if ((int)foodlist[i].AttackRangeMapGridID[v] == 0)
    //                        break;

    //                    GameObject mapgrid = null;
    //                    mapgrid = GetMapGridGo((int)foodlist[i].AttackRangeMapGridID[v]);
    //                    //Debug.Log(foodlist[i].AttackRangeMapGridID[v]);
    //                    mapgrid.GetComponent<MapGridView>().SetState(MapGridState.green);
    //                    lastFoodAttackRange.Add(foodlist[i].AttackRangeMapGridID[v]);
    //                }
    //            }
    //        }
    //    }

    //    //变红
    //    for (int i = 0; i < foodlist[0].AttackRangeMapGridID.Count; i++)
    //    {
    //        foodlist[0].isAttack = true;
    //        if ((int)foodlist[0].AttackRangeMapGridID[i] == 0)
    //            break;

    //        GameObject mapgrid = null;
    //        mapgrid = GetMapGridGo((int)foodlist[0].AttackRangeMapGridID[i]);
    //        //Debug.Log(foodlist[0].AttackRangeMapGridID[i]);
    //        mapgrid.GetComponent<MapGridView>().SetState(MapGridState.red);
    //        lastFoodAttackRange.Add(foodlist[0].AttackRangeMapGridID[i]);
    //    }

    //}

    /// <summary>
    /// 每回合开始，让行动的人攻击范围变红
    /// </summary>
    /// <param name="food"></param>
    //public void SetMapGridRed(Food food)
    //{
    //    for (int i = 0; i < food.AttackRangeMapGridID.Count; i++)
    //    {
    //        GameObject mapgrid = null;
    //        mapgrid = GetMapGridGo((int)food.AttackRangeMapGridID[i]);
    //        //Debug.Log(foodlist[0].AttackRangeMapGridID[i]);
    //        mapgrid.GetComponent<MapGridView>().SetState(MapGridState.red);
    //        lastFoodAttackRange.Add(food.AttackRangeMapGridID[i]);
    //    }
    //}


    /// <summary>
    /// 清除所有格子的显示
    /// </summary>
    public void ClearMapGridState()
    {
        for (int i = 0; i < mapGridDict.Count; i++)
        {
            GameObject mapgrid = null;
            mapgrid = GetMapGridGo(i);
            mapgrid.GetComponent<MapGridView>().SetState(MapGridState.normal);
            BlueMapGrid.Clear();
        }
        lastFoodAttackRange.Clear();
    }


    /// <summary>
    /// 显示一个FOOD的攻击范围
    /// </summary>
    /// <param name="food"></param>
    public void ShowRange(Food food)
    {
        for (int i = 0; i < food.AttackRangeMapGridID.Count; i++)
        {
            GameObject mapgrid = null;
            mapgrid = GetMapGridGo((int)food.AttackRangeMapGridID[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mgv.LastGridState = mgv.currenGridState;
            mapgrid.GetComponent<MapGridView>().SetState(MapGridState.gray);
            BlueMapGrid.Add(mapgrid);
        }
    }

    /// <summary>
    /// 隐藏一个FOOD的攻击范围
    /// </summary>
    /// <param name="food"></param>
    public void HideRange(Food food)
    {
        for (int i = 0; i < food.AttackRangeMapGridID.Count; i++)
        {
            GameObject mapgrid = null;
            mapgrid = GetMapGridGo((int)food.AttackRangeMapGridID[i]);
            MapGridView mgv = mapgrid.GetComponent<MapGridView>();
            mapgrid.GetComponent<MapGridView>().SetState(mgv.LastGridState);
        }
        BlueMapGrid.Clear();
    }

    
}
